package cn.tedu.yushougeproduct.controller;



import cn.tedu.yushouge.commons.pojo.vo.PageData;
import cn.tedu.yushouge.commons.web.JsonResult;
import cn.tedu.yushougeproduct.pojo.param.CommentAddNewParam;
import cn.tedu.yushougeproduct.pojo.param.CommentUpdateInfoParam;
import cn.tedu.yushougeproduct.pojo.vo.CommentListItemVO;
import cn.tedu.yushougeproduct.pojo.vo.CommentStandardVO;
import cn.tedu.yushougeproduct.service.ICommentService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Api(tags = "07. 用户评论管理模块")
@RequestMapping("/comment/")
@Slf4j
@Validated
public class CommentController {
    @Autowired
    private ICommentService commentService;

    @ApiOperation("添加用户评论")
    @PostMapping("add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid
                                     CommentAddNewParam commentAddNewParam) {
        commentService.addNew(commentAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("delete")
    @ApiOperation("根据ID删除用户评论")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户评论ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@Range(min = 1, message = "根据ID删除用户评论失败，请提交合法的ID值！")
                         @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID删除用户评论】的请求，参数：{}", id);
        commentService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改用户评论")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户评论ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@RequestParam @Range(min = 1, message = "请提交有效的用户评论ID值！") Long id,
                                           @Valid CommentUpdateInfoParam commentUpdateInfoParam) {
        log.debug("开始处理【修改用户评论详情】的请求，ID：{}", id);
        commentService.updateInfoById(id, commentUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("standard")
    @ApiOperation("根据ID查询用户评论详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户评论ID", required = true, dataType = "long")
    })
    public JsonResult<CommentStandardVO> getStandardById(@Range(min = 1, message = "根据ID查询用户评论详情失败，请提交合法的ID值！")
                             @RequestParam Long id) throws Exception {
        log.debug("开始处理【根据ID查询用户评论详情】的请求，参数：{}", id);
        CommentStandardVO result = commentService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("查询用户评论列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query")
    })
    public JsonResult<PageData<CommentListItemVO>> list(@Range(min = 1, message = "查询用户评论列表失败，请提供正确的页码值！") Integer pageNum) {
        log.debug("开始处理【查询用户评论列表】的请求，页码：{}", pageNum);
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        PageData<CommentListItemVO> pageData = commentService.list(pageNum);
        return JsonResult.ok(pageData);
    }
}


